'use strict';
/**
* Module dependencies.
*/
var inherit = require('./utils').inherit;
var isEmail = require('is-email');
var newDate = require('new-date');
var Facade = require('./facade');
/**
* Initialize a new `Group` facade with a `dictionary` of arguments.
*
* @param {Object} dictionary
* @param {string} userId
* @param {string} groupId
* @param {Object} properties
* @param {Object} options
* @param {Object} opts
* @property {boolean|undefined} clone
*/
function Group(dictionary, opts) {
Facade.call(this, dictionary, opts);
}
/**
* Inherit from `Facade`
*/
inherit(Group, Facade);
/**
* Get the facade's action.
*/
Group.prototype.action = function() {
return 'group';
};
Group.prototype.type = Group.prototype.action;
/**
* Setup some basic proxies.
*/
Group.prototype.groupId = Facade.field('groupId');
/**
* Get created or createdAt.
*
* @return {Date}
*/
Group.prototype.created = function() {
var created = this.proxy('traits.createdAt')
|| this.proxy('traits.created')
|| this.proxy('properties.createdAt')
|| this.proxy('properties.created');
if (created) return newDate(created);
};
/**
* Get the group's email, falling back to the group ID if it's a valid email.
*
* @return {string}
*/
Group.prototype.email = function() {
var email = this.proxy('traits.email');
if (email) return email;
var groupId = this.groupId();
if (isEmail(groupId)) return groupId;
};
/**
* Get the group's traits.
*
* @param {Object} aliases
* @return {Object}
*/
Group.prototype.traits = function(aliases) {
var ret = this.properties();
var id = this.groupId();
aliases = aliases || {};
if (id) ret.id = id;
for (var alias in aliases) {
var value = this[alias] == null ? this.proxy('traits.' + alias) : this[alias]();
Iif (value == null) continue;
ret[aliases[alias]] = value;
delete ret[alias];
}
return ret;
};
/**
* Special traits.
*/
Group.prototype.name = Facade.proxy('traits.name');
Group.prototype.industry = Facade.proxy('traits.industry');
Group.prototype.employees = Facade.proxy('traits.employees');
/**
* Get traits or properties.
*
* TODO: remove me
*
* @return {Object}
*/
Group.prototype.properties = function() {
return this.field('traits') || this.field('properties') || {};
};
/**
* Exports.
*/
module.exports = Group;
|